package chapter18;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.junit.Test;

public class RandomAccessFileTest {
	@Test
	public void testCreateFile(){
		String directory = "D:/test";
		String name = "t.txt";
		File f = new File(directory, name);
		RandomAccessFile file = null;
		try {
		file = new RandomAccessFile(f, "rw");
		byte[] b = {5,10,15,127};
		for(byte e:b){
			System.out.println(e);
		}
		try {
			//如果没有这行，文件也会生成，只是文件为空
			file.write(b,0,4);
		}
		catch (IOException e) {
			e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			}finally{
		if (null!=file){
			try {
				file.close();
			} catch (IOException e) {
			e.printStackTrace();
			}
			}

			}
	}
}
